﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using Ebada.UI.Base;
using Ebada.Client;
using Ebada.Core;
using Ebada.Client.Platform;
namespace Ebada.Modules.Vehicle
{
    /// <summary> 
    /// 违章记录编辑
    /// </summary>
    public partial class frmWarningTypeEdit : FormBase, IPopupFormEdit {
       

        public frmWarningTypeEdit() {
            InitializeComponent();
        }
        void dataBind() {

            this.txtAct_Code.DataBindings.Add("EditValue", rowData, "Act_Code");
            this.txtAct_Name.DataBindings.Add("EditValue", rowData, "Act_Name");
            this.txtDays.DataBindings.Add("EditValue", rowData, "Days");
            
        }
        #region IPopupFormEdit Members
        private VehicleWarningType rowData = null;

        public object RowData {
            get {
                return rowData;
            }
            set {
                if (value == null) return;
                if (rowData == null) {
                    this.rowData = value as VehicleWarningType;
                    dataBind();
                    InitComboBoxData();
                } else {
                    ConvertHelper.CopyTo<VehicleWarningType>(value as VehicleWarningType, rowData);
                    InitComboBoxData();
                }
            }
        }

        #endregion

        private void InitComboBoxData() {
            IList<Activities> list= MainHelper.GetSqlMap<Activities>().GetListByWhere<Activities>(" where ProjectName='车辆项目' order by Act_ID");
            txtAct_Name.Properties.DataSource = list;
        }
 
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtAct_Name.Text == "")
            {
                MessageBox.Show("作业名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtAct_Name.Focus();
                return;
            }
            if (txtDays.Text == "")
            {
                MessageBox.Show("预警提前天数不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtDays.Focus();
                return;
            }
            IList<VehicleWarningType> objlist = MainHelper.GetSqlMap<VehicleWarningType>().GetListByWhere<VehicleWarningType>(" where Act_Name='" + txtAct_Name.Text + "'");
            if (objlist.Count >0)
            {
                if (rowData.TypeID != objlist[0].TypeID)
                {
                    MessageBox.Show("选择的预警类型已经存在。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtAct_Name_EditValueChanged(object sender, EventArgs e)
        {
            if (txtAct_Name.Text != "")
            {
                rowData.Act_Code = ((Activities)txtAct_Name.Properties.GetDataSourceRowByKeyValue(txtAct_Name.Text)).Act_Code;
                txtAct_Code.Text = rowData.Act_Code;
            }
        }
    }
}